package com.amap.search.controller;

import com.alibaba.excel.EasyExcel;
import com.amap.search.entity.excel.AdCodeExcel;
import com.amap.search.entity.excel.PoiCodeExcel;
import com.amap.search.listener.AdCodeExcelListener;
import com.amap.search.listener.PoiCodeExcelListener;
import com.amap.search.mapper.db2022.AddressCodeMapper;
import com.amap.search.mapper.db2022.PoiCodeMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("excel")
@Api(tags = "Excel字典数据导入")
public class ExcelController {
    @Resource
    private AddressCodeMapper addressCodeMapper;
    @Resource
    private PoiCodeMapper poiCodeMapper;

    @Resource
    private AdCodeExcelListener adCodeExcelListener;

    @PostMapping("/import/adCode")
    @ApiOperation("导入行政区域字典数据")
    public String importAdCode(MultipartFile file) throws IOException {
        InputStream in = file.getInputStream();
        EasyExcel.read(in, AdCodeExcel.class, adCodeExcelListener).sheet().doRead();
        return "导入成功";
    }

    @PostMapping("import/poiCode")
    @ApiModelProperty("导入poi字典数据")
    public String importPoiCode(MultipartFile file) throws IOException{
        InputStream in = file.getInputStream();
        EasyExcel.read(in, PoiCodeExcel.class, new PoiCodeExcelListener(poiCodeMapper)).sheet().doRead();
        return "导入成功";
    }


}
